package xkp.lesson;

import java.io.*;

public class TestSerial {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Car car = new Car("豫A12345");
        Driver driver1 = new Driver("张三",20,car);

        //把driver1中的序列化到流中的一块内存中
        //FileOutputStream fileOutputStream = new FileOutputStream("objFile");
        //ByteArrayOutputStream对象中自带一块内存区域，可以把该内存区域作为数据源
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(driver1);
        objectOutputStream.flush();

        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        //从内存中取出来
        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
        Driver driver2 = (Driver) objectInputStream.readObject();
        objectInputStream.close();
        objectOutputStream.close();
        driver1.getCar().setCarNumber("123");
        System.out.println(driver1);
        System.out.println(driver2);
        //1、序列化到文件中
        //2、从文件中反序列化回来


    }
}
